package www.xfOnline.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import www.xfOnline.bean.User;
import www.xfOnline.com.XFOnline;
import www.xfOnline.dao.JDBCDAO;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.Insets;

public class XFOnlineFrame extends JFrame {

	private JPanel contentPane;
	public RegFrame regframe;
	public LoginFrame loginframe;
	public FixPersonDataFrame fixPersonDataframe;
	public MsgListFrame msgListframe;
	private JDBCDAO jdao;
	private User userMain;
	public JButton btnLogin;
	public JButton btnReg;
	public JButton btnExtends; 
	public JPanel panel_2;
	public JPanel panel_3;
	public JMenuBar menuBar;
	public JMenu personMenu;
	public JButton personSet;
	public JButton btnOut;
	private JLabel lblNewLabel_2;
	/**
	 * Create the frame.
	 */
	public XFOnlineFrame() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		jdao = new JDBCDAO();
		userMain = new User();
		Dimension scressSize = getToolkit().getScreenSize();
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds((int)scressSize.getWidth()/8, (int)scressSize.getHeight()/8, (int)scressSize.getWidth()/4*3, (int)scressSize.getHeight()/4*3);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.gridwidth = 2;
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		contentPane.add(panel, gbc_panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.WEST);
		
		JLabel lblNewLabel = new JLabel(new ImageIcon(getClass().getResource("/res/logo_big.png")));
		panel_1.add(lblNewLabel);
		
		panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.EAST);
		
		btnLogin = new JButton("登录");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(loginframe==null){
					loginframe = new LoginFrame(XFOnline.xfOnline,userMain);
				}
				loginframe.setVisible(true);
			}
		});
		panel_2.add(btnLogin);
		
		btnReg = new JButton("注册");
		btnReg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(regframe==null){
					regframe = new RegFrame();
				}
				regframe.setVisible(true);
			}
		});
		
		panel_3 = new JPanel();
		//panel_2.add(panel_3);
		
		menuBar = new JMenuBar();
		panel_3.add(menuBar);
		
		personMenu = new JMenu("name");
		menuBar.add(personMenu);
		
		personSet = new JButton("个人中心");
		personSet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(fixPersonDataframe==null){
					fixPersonDataframe = new FixPersonDataFrame(userMain);
				}
				fixPersonDataframe.setVisible(true);
			}
		});
		personMenu.add(personSet);
		
		btnOut = new JButton("退出");
		personMenu.add(btnOut);
		btnOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){

				personMenu.setPopupMenuVisible(false);
				panel_2.removeAll();
				panel_2.repaint();
				panel_2.add(btnLogin);
				panel_2.add(btnReg);
				panel_2.add(btnExtends);
			}
		});
		
		panel_2.add(btnReg);
		
		btnExtends = new JButton("待扩展");
		btnExtends.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,"待开发");
			}
		});
		panel_2.add(btnExtends);
		
		JLabel lblNewLabel_1 = new JLabel("                xfOnline购物天堂");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 30));
		panel.add(lblNewLabel_1, BorderLayout.CENTER);
		
		lblNewLabel_2 = new JLabel("管理员root密码123456");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_2.gridx = 1;
		gbc_lblNewLabel_2.gridy = 1;
		contentPane.add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
}
